﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Address_Book.DAL
{
    public class Friends
    {
        //读取配置文件
        Properties.Settings conn = Properties.Settings.Default;

        public DataSet Select(int index)
        {
            
            //利用using 关键字，让cn自动释放
            using (SqlConnection cn = new SqlConnection())
            {
                cn.ConnectionString = conn.SqlConnection;
                cn.Open();
                SqlDataAdapter da = new SqlDataAdapter("p_FriendsSelect", cn);//或 new SqlDataAdapter(); da.SelectCommand = new SqlCommand("p_FriendsSelect",cn);
                da.SelectCommand.CommandType = CommandType.StoredProcedure;//这个类型一定不能少。一不小心写漏了，还以为是储存过程出了问题……
                da.SelectCommand.Parameters.Add("@Index", SqlDbType.Int);
                da.SelectCommand.Parameters["@Index"].Value = index;
                DataSet ds = new DataSet();
                da.Fill(ds, "Friends");
                return ds;
            }
        }

        public bool UpData(Address_Book.Model.FriendInfo Finfo, string strOperate)
        {
            //利用using 关键字，让cn自动释放
            using (SqlConnection cn = new SqlConnection())
            {
                cn.ConnectionString = conn.SqlConnection;
                cn.Open();
                SqlCommand cm = new SqlCommand();
                cm.Connection = cn;
                cm.CommandType = CommandType.StoredProcedure;
                cm.Parameters.Add("@FID", SqlDbType.Int);
                cm.Parameters.Add("@FName", SqlDbType.NVarChar);
                cm.Parameters.Add("@FSex", SqlDbType.NVarChar);
                cm.Parameters.Add("@FBirthday", SqlDbType.NVarChar);
                cm.Parameters.Add("@FTel1", SqlDbType.NVarChar);
                cm.Parameters.Add("@FTel2", SqlDbType.NVarChar);
                cm.Parameters.Add("@FHomeTel", SqlDbType.NVarChar);
                cm.Parameters.Add("@FQQ", SqlDbType.NVarChar);
                cm.Parameters.Add("@FE_Mail", SqlDbType.NVarChar);
                cm.Parameters.Add("@FAddress", SqlDbType.NVarChar);
                cm.Parameters.Add("@FLelever", SqlDbType.NVarChar);
                cm.Parameters.Add("@FNote", SqlDbType.NVarChar);

                cm.CommandText = strOperate;
                cm.Parameters["@FID"].Value = Finfo.FID;
                cm.Parameters["@FName"].Value = Finfo.FName;
                cm.Parameters["@FSex"].Value = Finfo.FSex;
                cm.Parameters["@FBirthday"].Value = Finfo.FBirthday;
                cm.Parameters["@FTel1"].Value = Finfo.FTel1;
                cm.Parameters["@FTel2"].Value = Finfo.FTel2;
                cm.Parameters["@FHomeTel"].Value = Finfo.FHomeTel;
                cm.Parameters["@FQQ"].Value = Finfo.FQQ;
                cm.Parameters["@FE_Mail"].Value = Finfo.FE_Mail;
                cm.Parameters["@FAddress"].Value = Finfo.FAddress;
                cm.Parameters["@FLelever"].Value = Finfo.FLelever;
                cm.Parameters["@FNote"].Value = Finfo.FNote;
                
                try
                {
                    cm.ExecuteNonQuery();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
 
        }
    }
}
